package wiz.client.activities.playgame;

import wiz.client.ClientFactory;
import wiz.client.activities.GameScorePresenter;
import wiz.client.activities.finishedgame.FinishedGamePlace;
import wiz.client.activities.playhand.PlayHandPlace;
import wiz.client.events.RoundAddedEvent;
import wiz.client.events.RoundStartedEvent;
import wiz.client.model.WizGameModel;
import wiz.client.model.WizGameRound;
import wiz.client.model.WizPlayer;
import wiz.client.model.WizPlayerRoundScore;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.AcceptsOneWidget;

public class PlayGameActivity extends AbstractActivity{

	private ClientFactory fClientFactory;
	private GameSummaryView fView;
	
	private String fLastGameStateToken;
	private GameScorePresenter fGameScorePresenter;
	
	public PlayGameActivity(ClientFactory clientFactory) {
		fClientFactory = clientFactory;
		GameSummaryView view = fClientFactory.buildGameSummaryView();
		fView = view;
		
		fGameScorePresenter = new GameScorePresenter(clientFactory, fView.getGameScoreView());
		
		initHandlers();


	}

	@Override
	public void start(AcceptsOneWidget panel, EventBus eventBus) {
		WizGameModel loadGameState = fClientFactory.getGameStateStorage().loadGameState(fLastGameStateToken);
		if(loadGameState.getGameRounds().size() == 0){
			startRoundClicked();
		}
		
		panel.setWidget(fView);
		Window.scrollTo(0, Window.getClientHeight());
	}

	private void initHandlers() {
		fView.getDoneGameClickHandlers().addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				doneGameClicked();
				
			}
		});
		
		fView.getStartNewRoundClickHandlers().addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				startRoundClicked();
			}
		});
	}

	protected void startRoundClicked() {
		WizGameModel loadGameState = fClientFactory.getGameStateStorage().loadGameState(fLastGameStateToken);
		WizGameRound newRound = fClientFactory.getWizModelFactory().createRound();
		loadGameState.getGameRounds().add(newRound);
		
		//initialize round with player scores
		for (WizPlayer player : loadGameState.getWizPlayers()) {
			WizPlayerRoundScore createPlayerRoundScore = fClientFactory.getWizModelFactory().createPlayerRoundScore();
			createPlayerRoundScore.setPlayer(player);
			createPlayerRoundScore.setTricksBid(0);
			createPlayerRoundScore.setTricksMade(0);
			newRound.getRoundScores().add(createPlayerRoundScore);
		}
		
		
		
		fLastGameStateToken = fClientFactory.getGameStateStorage().storeGameState(loadGameState);
		fClientFactory.getPlaceController().goTo(new PlayHandPlace(fLastGameStateToken));
		
		fClientFactory.getEventBus().fireEvent(new RoundAddedEvent());

		
	}

	protected void doneGameClicked() {
		fClientFactory.getPlaceController().goTo(new FinishedGamePlace(fLastGameStateToken));
	}

	public void setGameStateToken(String gameStateToken) {
		if(gameStateToken == null){
			throw new IllegalArgumentException();
		}
		
		fLastGameStateToken = gameStateToken;
		WizGameModel loadGameState = fClientFactory.getGameStateStorage().loadGameState(fLastGameStateToken);
		
		fGameScorePresenter.displayGameState(loadGameState);	
		
		
	}



	

}
